{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9d30fdd1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "地域csv路径: E:\\oo\\area\n",
      "类型csv路径: E:\\oo\\csv\n",
      "图片保存路径: E:\\oo\\img\n",
      "\n",
      "当前群系信息 216_岗松灌丛_岗松灌丛_216\n",
      "\n",
      "当前指定地域及类型:\n",
      "地域 1 ['类型1', '类型2']\n",
      "\n",
      "当前团簇地域及类型:没有\n",
      "\n",
      "当前强制水平垂直拟合地域及类型:没有\n",
      "\n",
      "当前图例位置 上右\n",
      "\n",
      "分段拟合 否\n",
      "手动二段线 否\n",
      "手动三段线 否\n",
      "\n",
      "当前csv:1.1.0.1.csv\n",
      "value值和data数据内不一致\n",
      "总样本点数：988\n",
      "分区后的总样本点数：111\n",
      "\n",
      "当前csv:1.1.0.2.csv\n",
      "value值和data数据内不一致\n",
      "总样本点数：988\n",
      "分区后的总样本点数：70\n",
      "\n",
      "当前csv:1.1.0.3.csv\n",
      "\n",
      "当前csv:1.1.0.4.csv\n",
      "\n",
      "当前csv:1.1.0.5.csv\n",
      "\n",
      "当前csv:1.1.0.6.csv\n",
      "\n",
      "当前csv:1.1.0.7.csv\n",
      "\n",
      "各csv点数之和与该群系总点数不相等,请检查:\n",
      "1.该群系的所有csv是否同时跑了\n",
      "2.是否有漏点情况\n",
      "\n",
      "开始生成信息统计csv\n",
      "--------分割线--------\n",
      "开始生成格子图\n",
      "{'1.1.0.1.csv': {'k_list': [{'k': -10.961453544592851, 'b': 68.78117756504321, 'r': 0.532548064312159, 'type': 'normal'}], 'writel': {0: (0, 30), 1: (0, 30), 2: (0, 30)}}, '1.1.0.2.csv': {'k_list': [{'k': -14.606044595186571, 'b': 78.30474956214411, 'r': 0.9220021455032448, 'type': 'normal'}], 'writel': {0: (0, 30), 1: (0, 30), 2: (0, 30)}}, '1.1.0.3.csv': {}, '1.1.0.4.csv': {}, '1.1.0.5.csv': {}, '1.1.0.6.csv': {}, '1.1.0.7.csv': {}}\n",
      "预估完成时间：犹未可知\n",
      "格子图拟合\n",
      "格子图总体拟合\n",
      "进度7/7，预估完成时间Fri Nov  4 09:21:27 2022\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "3.31 修复bug\n",
    "3.29 出信息统计csv\n",
    "5.12 14.34 两段线会出bug不过被修复了\n",
    "5.12 20.28 加了一下对当前设置的提醒,设置跟settings.py不匹配请重启内核\n",
    "5.12 21.59 多段拟合用水平线近似会出bug不过被解决了\n",
    "5.13 20.14 如果点很少会报错不过现在不报了\n",
    "5.14 17.32 加了一个手动垂直水平拟合的变量，解决需要垂直水平拟合但是没有自动弹窗的问题\n",
    "\n",
    "5.15 20.27 特殊情况：如果点csv跨区会出现两条拟合线，如果此时需要用一条水平/垂直线拟合时，水平拟合输3，垂直拟合输4\n",
    "\n",
    "5.24 1931  修复了分段拟合的一个bug，三段拟合中间那条线现在可以用水平拟合了（大概\n",
    "6.1 23.42 垂线+斜线\n",
    "6.2 18.20 修复5.15写的bug\n",
    "6.13 10.09 在指定某个类型的时候可能会出现格子图前后不一样的状况，被修复了\n",
    "\n",
    "11.2 添加4条额外的p/pe线\n",
    "\n",
    "11.3 修复标注重复的问题\n",
    "11.4\n",
    "\"\"\"\n",
    "\n",
    "import csv\n",
    "import time\n",
    "import os\n",
    "import gc\n",
    "\n",
    "import matplotlib.pylab as plt\n",
    "from lat import ExecuteCsv_rewrite\n",
    "from settings import area_csv_path,type_dict,csv_path,is_split,List,loc,save_path,cluster_dict,bi_manual,tri_manual,manual_vert_hori_dict\n",
    "from order import  file_name,read_xy, data_process,figure_drawing,judge_p,headers,re_figure_drawing\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "img_save_path = save_path\n",
    "csvpath =csv_path#运行csv所在的文件夹，该程序运行的是该文件夹下的所有的csv文件\n",
    "name2 = List.split(\"_\")[-1] + \"_\" + List.split(\"_\")[-2]\n",
    "total = 0\n",
    "\n",
    "rows=[]\n",
    "\n",
    "csv_number=0\n",
    "\n",
    "print(\"地域csv路径:\",area_csv_path)\n",
    "print(\"类型csv路径:\",csv_path)\n",
    "print(\"图片保存路径:\",save_path)\n",
    "print()\n",
    "print(\"当前群系信息\",List)\n",
    "print()\n",
    "if type_dict:\n",
    "    print(\"当前指定地域及类型:\")\n",
    "    for i in type_dict:\n",
    "        if type_dict[i]:\n",
    "            print(\"地域\",i,[\"类型\"+j for j in type_dict[i]])\n",
    "        else:\n",
    "            print(\"地域\",i,\"全部\")\n",
    "else:\n",
    "    print(\"当前指定地域及类型:不指定\")\n",
    "print()\n",
    "if cluster_dict:\n",
    "    print(\"当前团簇地域及类型:\")\n",
    "    for i in cluster_dict:\n",
    "        if cluster_dict[i]:\n",
    "            print(\"地域\",i,[\"类型\"+j for j in cluster_dict[i]])\n",
    "        else:\n",
    "            print(\"地域\",i,\"全部\")\n",
    "else:\n",
    "    print(\"当前团簇地域及类型:没有\")    \n",
    "print()\n",
    "\n",
    "if manual_vert_hori_dict:\n",
    "    print(\"当前强制水平垂直拟合地域及类型:\")\n",
    "    for i in manual_vert_hori_dict:\n",
    "        if manual_vert_hori_dict[i]:\n",
    "            print(\"地域\",i,[\"类型\"+j for j in manual_vert_hori_dict[i]])\n",
    "        else:\n",
    "            print(\"地域\",i,\"全部\")\n",
    "else:\n",
    "    print(\"当前强制水平垂直拟合地域及类型:没有\")   \n",
    "print()\n",
    "print(\"当前图例位置\",loc.replace(\"upper\",\"上\").replace(\"left\",\"左\").replace(\"lower\",\"下\").replace(\"right\",\"右\").replace(\" \",\"\"))\n",
    "print()\n",
    "yeslist = [\"否\",\"是\"]\n",
    "print(\"分段拟合\",yeslist[is_split])\n",
    "print(\"手动二段线\",yeslist[bi_manual])    \n",
    "print(\"手动三段线\",yeslist[tri_manual])\n",
    "\n",
    "\n",
    "\n",
    "if not is_split:\n",
    "    for i in [List]:\n",
    "        k_dict = {}\n",
    "        k_dict_total = {}\n",
    "\n",
    "        str1=i.split('_')[0]#value\n",
    "        str2=i.split('_')[1]#name\n",
    "        str3=i.split('_')[2]#说明书上的名字\n",
    "        str4=i.split('_')[3]#说明书上的群系编号\n",
    "        #csv名称\n",
    "        str8=str4+ '_' +str3+'_各类型信息统计'\n",
    "        str9=str4+ '_' +str3+'_分段拟合_各类型信息统计'\n",
    "        str6=0\n",
    "        #str6='椴、槭林'#输出二级分类的csv名\n",
    "        #         csvpath=r'F:\\2csv\\6121'#运行csv所在的文件夹，该程序运行的是该文件夹下的所有的csv文件\n",
    "\n",
    "        L,name=file_name(csvpath)\n",
    "        path=save_path+r'\\总体拟合（有序图）'\n",
    "\n",
    "        if not os.path.exists(path):\n",
    "            os.mkdir(path)\n",
    "        if not os.path.exists(save_path+r'\\有序图'):\n",
    "            os.mkdir(save_path+r'\\有序图')\n",
    "\n",
    "        for str5 in name:\n",
    "            L=[]\n",
    "            print('\\n当前csv:'+str5)\n",
    "            if str5[:-4] == str8 or str5[:-4]==str9 :\n",
    "                continue\n",
    "\n",
    "        ####################植被小区的名字，改改改#################\n",
    "            str5_1=str5.split('.')[1]#大地域编号\n",
    "\n",
    "            if str5_1 not in type_dict and type_dict:\n",
    "                k_dict[str5] = {}\n",
    "                k_dict_total[str5] = {}\n",
    "                continue\n",
    "        #########################################################\n",
    "\n",
    "            str5_2_1=str5.split('.')[0]#温度带\n",
    "            str5_3=str5.split('.')[2]#地域\n",
    "            str5_4=str5.split('.')[3].split('_')[0]#子类型\n",
    "            if  type_dict:\n",
    "                if str5_4 not in type_dict[str5_1] and  type_dict[str5_1]:\n",
    "                    k_dict[str5] = {}\n",
    "                    k_dict_total[str5] = {}\n",
    "                    continue    \n",
    "            str5_1='地域'+str5_1\n",
    "            temprList = ['温度交错带','高山极地带', '苔原带', '寒温带', '中温带', '暖温带', '亚热带', '热带','s22','s22']\n",
    "\n",
    "            if str5_4 == '1123':\n",
    "                if str5_1=='':\n",
    "                    str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]#输出图片名\n",
    "                else:\n",
    "                    str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'_'+str5_1#输出图片名\n",
    "            else:\n",
    "                if str5_1=='':\n",
    "                    if str5_3=='0':\n",
    "                        str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'_'+'类型'+str5_4#输出图片名\n",
    "                    else:\n",
    "                        str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'-'+str5_3+'_'+'类型'+str5_4#输出图片名\n",
    "                else:\n",
    "                    if str5_3=='0':\n",
    "                        str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+ '_'+str5_1+'_'+'类型'+str5_4#输出图片名\n",
    "                    else:\n",
    "                        str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+ '_'+str5_1+'-'+str5_3+'_'+'类型'+str5_4#输出图片名\n",
    "\n",
    "            tmix_list=[]\n",
    "            tmix_str=''\n",
    "            if str5_2_1 == '0':\n",
    "                tmix=str5.split('_')[1][:-4]\n",
    "                tmix_list=tmix.split('.')\n",
    "                for TL in range(len(tmix_list)):\n",
    "                    tmix_list[TL]  = temprList[int(tmix_list[TL])]\n",
    "                tmix_str='-'.join(tmix_list)\n",
    "                str7=str7.replace('温度',tmix_str)\n",
    "\n",
    "            x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year= read_xy(csvpath+'/%s'%str5)\n",
    "\n",
    "            #检查data数据导出时命名是否正确\n",
    "            check_value = check[0]\n",
    "            if check_value != int(str1):\n",
    "                    print('value值和data数据内不一致')\n",
    "            all_value = data_process(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year,0.05)\n",
    "            print('总样本点数：%d'%all_value['n'])\n",
    "            print('分区后的总样本点数：%d'%all_value['n_check'])\n",
    "            fig_opt = {'title': ('$%s$'+ ' '+u'%s'+str7.replace(str4+'_'+str3,'').replace('_',' '))%(str4,str3)}   \n",
    "\n",
    "            #分类型拟合\n",
    "            fig, ax = plt.subplots(figsize = (17,13))\n",
    "            figure_drawing,regression2,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4,k_list= figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, 2,0.05,fig,ax,str5_4,str5_2_1,**fig_opt)\n",
    "            if not k_list:\n",
    "                k_list = []\n",
    "            fig_save_address1=save_path.replace('\\\\','/')+'/有序图/%s.png'\n",
    "            plt.savefig(fig_save_address1%(str7.replace(\"/\",\"_\")),dpi = 300,bbox_inches='tight')\n",
    "            plt.clf()\n",
    "            plt.close('all')#清除当前画布，减少内存\n",
    "            ischange = False\n",
    "            for index,j in enumerate(k_list):\n",
    "                if j[\"r\"]<0.1 or (str5.split('.')[1] in manual_vert_hori_dict and (str5_4 in manual_vert_hori_dict[str5.split('.')[1]] or not manual_vert_hori_dict[str5.split('.')[1]])):\n",
    "                    is_hexline = input(\"【拟合很差】图%s第%s条线拟合很差，是否用水平或垂线替代？\\n水平替代输入1，垂线替代输2，不替代输入0，特殊情况(见代码头部)输其他\"%(str7,index))\n",
    "                    if is_hexline == \"1\":\n",
    "                        j[\"type\"] = \"horizon\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                    if is_hexline == \"2\":\n",
    "                        j[\"type\"] = \"vertical\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                    if is_hexline == \"3\":\n",
    "                        j[\"type\"] = \"horizon\"\n",
    "                        k_list = [j]\n",
    "                        ischange = True\n",
    "                        break\n",
    "                    if is_hexline == \"4\":\n",
    "                        j[\"type\"] = \"vertical\"\n",
    "                        k_list = [j]\n",
    "                        ischange = True\n",
    "                        break\n",
    "                elif j[\"type\"] == \"normal\" and abs(j[\"k\"]) <2 and j[\"r\"]<0.3:\n",
    "                    is_hexline = input(\"【拟合较差】图%s第%s条线可能近似水平线，是否用水平线替代？\\n确定输入1  不是输入0 用垂线替代输9 \"%(str7,index))\n",
    "                    if is_hexline == \"1\":\n",
    "                        j[\"type\"] = \"horizon\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                    if is_hexline == \"9\":\n",
    "                        j[\"type\"] = \"vertical\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                elif j[\"type\"] == \"normal\" and abs(j[\"k\"]) >30 and j[\"r\"]<0.3:\n",
    "                    is_hexline = input(\"【拟合较差】图%s第%s条线可能近似垂直线，是否用垂直线替代？\\n确定输入1  不是输入0  用水平线替代输9\"%(str7,index))\n",
    "                    if is_hexline == \"1\":\n",
    "                        j[\"type\"] = \"vertical\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                    if is_hexline == \"9\":\n",
    "                        j[\"type\"] = \"horizon\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "            k_dict[str5] = {\"k_list\" :k_list,\"writel\":{0 : (0,30) }}\n",
    "            writel = {0 : (0,30) }\n",
    "            overjump = False\n",
    "            if ischange:\n",
    "                fig, ax = plt.subplots(figsize = (17,13))\n",
    "\n",
    "                figure_drawing,regression2,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4 ,writel,k_list= re_figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, 2,0.05,fig,ax,str5_4,str5_2_1,k_list,csvpath+'/%s'%str5,**fig_opt)\n",
    "                overjump = False\n",
    "\n",
    "                for i in k_dict[str5][\"k_list\"]:\n",
    "                    if i[\"type\"] == \"vertical\" and len(k_dict[str5][\"k_list\"]) == 2:\n",
    "                        overjump = True\n",
    "                if not overjump:        \n",
    "                    k_dict[str5][\"k_list\"] = k_list\n",
    "                k_dict[str5][\"writel\"] = writel\n",
    "                fig_save_address1=save_path.replace('\\\\','/')+'/有序图/%s.png'\n",
    "                plt.savefig(fig_save_address1%(str7.replace(\"/\",\"_\")),dpi = 300,bbox_inches='tight')\n",
    "                plt.clf()\n",
    "                plt.close('all')#清除当前画布，减少内存\n",
    "\n",
    "            if not overjump:        \n",
    "                figure_drawing2,regression22,x_12,y_12,x_22,y_22,x_32,y_23,x_24,y_24 ,writel,k_list2= re_figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, 2,0.05,fig,ax,str5_4,str5_2_1,k_list,csvpath+'/%s'%str5,writel = writel,**fig_opt)\n",
    "\n",
    "            overjump = False\n",
    "            for i in k_dict[str5][\"k_list\"]:\n",
    "                if i[\"type\"] == \"vertical\" and len(k_dict[str5][\"k_list\"]) == 2:\n",
    "                    overjump = True\n",
    "            if not overjump:        \n",
    "                k_dict[str5][\"writel\"] = writel\n",
    "            #总体拟合\n",
    "\n",
    "            fig, ax = plt.subplots(figsize = (17,13))\n",
    "\n",
    "            figure_drawing1,regression1,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4,k_list= figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, 1,0.05,fig,ax,str5_4,str5_2_1, **fig_opt)    \n",
    "            k_dict_total[str5] = {\"k_list\" :k_list,\"writel\":{0: (0, 30), 1: (0, 30), 2: (0, 30)}}\n",
    "            k_dict_total[str5][\"k_list\"] = k_list\n",
    "            fig_save_address2=path.replace('\\\\','/')+'/%s.png'  \n",
    "            plt.savefig(fig_save_address2%(str7.replace(\"/\",\"_\")),dpi = 300,bbox_inches='tight')\n",
    "            plt.clf()\n",
    "\n",
    "            plt.close('all')\n",
    "            if len(regression2)==1:\n",
    "                regression2.append(['/','/','/'])\n",
    "            csv_number+=all_value['n_check']\n",
    "            L=[str2,str1,str3,str4,total,all_value['n_check'],temprList[int(str5_2_1)].replace('温度',tmix_str),\n",
    "               str5_1[2:],str5_3,str5_4,\n",
    "               regression1[0][2].replace('$','').split('(')[0],regression1[0][0],regression1[0][1],judge_p(regression1[0][1]),\n",
    "               regression2[0][2].replace('$','').split('(')[0],regression2[0][0],regression2[0][1],judge_p(regression2[0][1]),\n",
    "               regression2[1][2].replace('$','').split('(')[0],regression2[1][0],regression2[1][1],judge_p(regression2[1][1]),\n",
    "              all_value['TB_max'],all_value['TB_min'],all_value['TB_mean'],all_value['TB_std'],\n",
    "              all_value['E_max'],all_value['E_min'],all_value['E_mean'],all_value['E_std'],\n",
    "              all_value['p_max'],all_value['p_min'],all_value['p_mean'],all_value['p_std'],\n",
    "              all_value['pe_max'],all_value['pe_min'],all_value['pe_mean'],all_value['pe_std'],\n",
    "              all_value['DEM_max'],all_value['DEM_min'],all_value['DEM_mean'],all_value['DEM_std'],\n",
    "              all_value['tp_year_max'],all_value['tp_year_min'],all_value['tp_year_mean'],all_value['tp_year_std']]\n",
    "            rows.append(L)\n",
    "    if csv_number==total:\n",
    "        print('\\n各csv点数之和与该群系总点数相等')\n",
    "    else:\n",
    "        print('\\n各csv点数之和与该群系总点数不相等,请检查:\\n1.该群系的所有csv是否同时跑了\\n2.是否有漏点情况')\n",
    "    \n",
    "    print('\\n开始生成信息统计csv\\n--------分割线--------')\n",
    "    csv_save_address=save_path.replace('\\\\','/')+'/%s.csv'\n",
    "    with open(csv_save_address%(str8.replace(\"/\",\"_\")),'w',newline='') as f1:\n",
    "        f1_csv = csv.writer(f1)\n",
    "        f1_csv.writerow(headers)\n",
    "        f1_csv.writerows(rows)\n",
    "    print('开始生成格子图')\n",
    "\n",
    "    \n",
    "    \n",
    "else:\n",
    "    k_dict = {}\n",
    "    k_dict_total = {}\n",
    "    L,name=file_name(csvpath)\n",
    "    for str5 in name:\n",
    "        k_dict[str5] = {}\n",
    "        k_dict_total[str5] = {}\n",
    "        \n",
    "    str1=List.split('_')[0]#value\n",
    "    str2=List.split('_')[1]#name\n",
    "    str3=List.split('_')[2]#说明书上的名字\n",
    "    str4=List.split('_')[3]#说明书上的群系编号\n",
    "    TSL='1'\n",
    "    headers1=['name','value','说明书名字','说明书群系编号','总样本点数','分区后总样本点数','温度带','区域','小地域','类型',\n",
    "             '分界点1坐标','分界点2坐标','分界点3坐标','分界点4坐标',\n",
    "             '第一段回归方程','R方','P值','P值范围',\n",
    "             '第二段回归方程','R方','P值','P值范围',\n",
    "              '第三段回归方程','R方','P值','P值范围',\n",
    "            'TB_max','TB_min','TB_mean','TB_std',\n",
    "            'E_max','E_min','E_mean','E_std',\n",
    "            'p_max','p_min','p_mean','p_std',\n",
    "            'pe_max','pe_min','pe_mean','pe_std',\n",
    "            'dem_max','dem_min','dem_mean','dem_std',\n",
    "            'tp_year_max','tp_year_min','tp_year_mean','tp_year_std']\n",
    "    rows1=[]\n",
    "    TS_path=save_path+r'\\分段拟合（有序图）'\n",
    "    if not os.path.exists(TS_path):\n",
    "        os.mkdir(TS_path)\n",
    "    else:\n",
    "        print('分段拟合文件夹已创建')\n",
    "    \n",
    "    ##自行选取csv进行拟合\n",
    "    while TSL=='1' :\n",
    "        L=[]\n",
    "    \n",
    "        str5=input('输入当前文件夹目录下需要进行分段拟合的csv名称，示例:6.1.0.3\\n')+'.csv'\n",
    "        \n",
    "        str5_1=str5.split('.')[1]#大地域编号\n",
    "        str5_1='地域'+str5_1\n",
    "        str5_2_1=str5.split('.')[0]#温度带\n",
    "        str5_3=str5.split('.')[2]#地域\n",
    "        str5_4=str5.split('.')[3].split('_')[0]#子类型\n",
    "        str9=str4+ '_' +str3+'_分段拟合_各类型信息统计'   \n",
    "        temprList = ['温度交错带','高山极地带', '苔原带', '寒温带', '中温带', '暖温带', '亚热带', '热带']\n",
    "                \n",
    "        if str5_4 == '0':\n",
    "            if str5_1=='':\n",
    "                str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]#输出图片名\n",
    "            else:\n",
    "                str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'_'+str5_1#输出图片名\n",
    "        else:\n",
    "            if str5_1=='':\n",
    "                if str5_3=='0':\n",
    "                    str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'_'+'类型'+str5_4#输出图片名\n",
    "                else:\n",
    "                    str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'-'+str5_3+'_'+'类型'+str5_4#输出图片名\n",
    "            else:\n",
    "                if str5_3=='0':\n",
    "                    str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+ '_'+str5_1+'_'+'类型'+str5_4#输出图片名\n",
    "                else:\n",
    "                    str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+ '_'+str5_1+'-'+str5_3+'_'+'类型'+str5_4#输出图片名\n",
    "                \n",
    "        tmix_list=[]\n",
    "        tmix_str=''\n",
    "        if str5_2_1 == '0':\n",
    "            tmix=str5.split('_')[1][:-4]\n",
    "            tmix_list=tmix.split('.')\n",
    "            for TL in range(len(tmix_list)):\n",
    "                tmix_list[TL]  = temprList[int(tmix_list[TL])]\n",
    "            tmix_str='-'.join(tmix_list)\n",
    "            str7=str7.replace('温度',tmix_str)\n",
    "        \n",
    "        #读取数据\n",
    "        x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year= read_xy(csvpath+'/%s'%str5)\n",
    "        check_value = check[0]\n",
    "        if check_value != int(str1):\n",
    "            print('value值和data数据内不一致')\n",
    "        all_value = data_process(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year,0.05)\n",
    "        print('总样本点数：%d'%all_value['n'])\n",
    "        print('分区后的总样本点数：%d'%all_value['n_check'])   \n",
    "        fig_opt = {'title': ('$%s$'+ ' '+u'%s'+str7.replace(str4+'_'+str3,'').replace('_',' '))%(str4,str3)}   \n",
    "        control_fd=0\n",
    "        while control_fd==0:\n",
    "            \n",
    "        #分类型拟合\n",
    "            fig, ax = plt.subplots(figsize = (17,13))\n",
    "            figure_drawing,regression2,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4,k_list= figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, TSL,0.05,fig,ax,str5_4,str5_2_1,**fig_opt)\n",
    "            fig_save_address1=TS_path.replace('\\\\','/')+'/%s.png'\n",
    "            \n",
    "            plt.savefig(fig_save_address1%(str7.replace(\"/\",\"_\")),dpi = 300,bbox_inches='tight')    \n",
    "            plt.clf()\n",
    "            plt.close('all')#清除当前画布，减少内存\n",
    "            ischange = False\n",
    "            for index,j in enumerate(k_list):\n",
    "                if j[\"r\"]<0.1 or (str5.split('.')[1] in manual_vert_hori_dict and (str5_4 in manual_vert_hori_dict[str5.split('.')[1]] or not manual_vert_hori_dict[str5.split('.')[1]])):\n",
    "                    is_hexline = input(\"【拟合很差】图%s第%s条线拟合很差，是否用水平或垂线替代？\\n水平替代输入1，垂线替代输2，不替代输入0\"%(str7,index))\n",
    "                    if is_hexline == \"1\":\n",
    "                        j[\"type\"] = \"horizon\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                    if is_hexline == \"2\":\n",
    "                        j[\"type\"] = \"vertical\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                elif j[\"type\"] == \"normal\" and abs(j[\"k\"]) <2 and j[\"r\"]<0.3:\n",
    "                    is_hexline = input(\"【拟合较差】图%s第%s条线可能近似水平线，是否用水平线替代？\\n确定输入1  不是输入0 用垂线替代输9 \"%(str7,index))\n",
    "                    if is_hexline == \"1\":\n",
    "                        j[\"type\"] = \"horizon\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                    if is_hexline == \"9\":\n",
    "                        j[\"type\"] = \"vertical\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                elif j[\"type\"] == \"normal\" and abs(j[\"k\"]) >30 and j[\"r\"]<0.3:\n",
    "                    is_hexline = input(\"【拟合较差】图%s第%s条线可能近似垂直线，是否用垂直线替代？\\n确定输入1  不是输入0  用水平线替代输9\"%(str7,index))\n",
    "                    if is_hexline == \"1\":\n",
    "                        j[\"type\"] = \"vertical\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                    if is_hexline == \"9\":\n",
    "                        j[\"type\"] = \"horizon\"\n",
    "                        k_list[index] = j\n",
    "                        ischange = True\n",
    "                        \n",
    "                    \n",
    "            k_dict[str5] = {\"k_list\" :k_list}\n",
    "            fig, ax = plt.subplots(figsize = (17,13))\n",
    "\n",
    "            figure_drawing,regression3,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4 ,writel,k_list= re_figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year,  TSL,0.05,fig,ax,str5_4,str5_2_1,k_list,csvpath+'/%s'%str5,xykey = [x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4],**fig_opt)\n",
    "\n",
    "            k_dict[str5][\"k_list\"] = k_list\n",
    "            \n",
    "            plt.clf()\n",
    "            plt.close('all')#\n",
    "\n",
    "            fig, ax = plt.subplots(figsize = (17,13))\n",
    "            #figure_drawing2,regression32,x_12,y_12,x_22,y_22,x_32,y_32,x_24,y_24 ,writel,k_list= re_figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year,  TSL,0.05,fig,ax,str5_4,str5_2_1,k_list,csvpath+'/%s'%str5,xykey = [x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4],**fig_opt)\n",
    "            figure_drawing,regression3,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4 ,writel,k_list= re_figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year,  TSL,0.05,fig,ax,str5_4,str5_2_1,k_list,csvpath+'/%s'%str5,xykey = [x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4],**fig_opt)\n",
    "            fig_save_address1=TS_path.replace('\\\\','/')+'/%s.png'\n",
    "            plt.savefig(fig_save_address1%(str7.replace(\"/\",\"_\")),dpi = 300,bbox_inches='tight')    \n",
    "            \n",
    "            k_dict[str5][\"k_list\"] = k_list\n",
    "            plt.clf()\n",
    "            plt.close('all')#清除当前画布，减少内存\n",
    "            k_dict[str5][\"writel\"] = writel\n",
    "            \n",
    "            \n",
    "            control_fd=int(input('当前分界线是否合适(1为合适,0为不合适)'))\n",
    "        L=[str2,str1,str3,str4,total,all_value['n_check'],temprList[int(str5_2_1)].replace('温度',tmix_str),\n",
    "           str5_1[2:],str5_3,str5_4,\n",
    "           '('+str(x_1)+','+str(y_1)+')','('+str(x_2)+','+str(y_2)+')','('+str(x_3)+','+str(y_3)+')','('+str(x_4)+','+str(y_4)+')',\n",
    "           regression2[0][2].replace('$','').split('(')[0][:-3],regression2[0][0],regression2[0][1],judge_p(regression2[0][1]),\n",
    "           regression2[1][2].replace('$','').split('(')[0][:-3],regression2[1][0],regression2[1][1],judge_p(regression2[1][1]),\n",
    "           regression2[2][2].replace('$','').split('(')[0][:-3],regression2[2][0],regression2[2][1],judge_p(regression2[2][1]),\n",
    "           all_value['TB_max'],all_value['TB_min'],all_value['TB_mean'],all_value['TB_std'],\n",
    "           all_value['E_max'],all_value['E_min'],all_value['E_mean'],all_value['E_std'],\n",
    "           all_value['p_max'],all_value['p_min'],all_value['p_mean'],all_value['p_std'],\n",
    "           all_value['pe_max'],all_value['pe_min'],all_value['pe_mean'],all_value['pe_std'],\n",
    "           all_value['DEM_max'],all_value['DEM_min'],all_value['DEM_mean'],all_value['DEM_std'],\n",
    "           all_value['tp_year_max'],all_value['tp_year_min'],all_value['tp_year_mean'],all_value['tp_year_std']]\n",
    "        rows1.append(L)\n",
    "        TSL=input('是否仍需进行分段拟合:')\n",
    "    \n",
    "    \n",
    "\n",
    "    print('\\n开始生成分段拟合信息统计csv\\n--------分割线--------')\n",
    "    csv_save_address=TS_path.replace('\\\\','/')+'/%s.csv'\n",
    "    c=1\n",
    "    while c == 1:\n",
    "        try:\n",
    "            with open(csv_save_address%(str9.replace(\"/\",\"_\")),'w',newline='') as f1:\n",
    "                f1_csv = csv.writer(f1)\n",
    "                f1_csv.writerow(headers1)\n",
    "                f1_csv.writerows(rows1)\n",
    "                c = 0 \n",
    "        except PermissionError:\n",
    "            input('csv未关闭，关闭csv后输入任意键重试')\n",
    "\n",
    "#格子图\n",
    "\n",
    "print(k_dict)\n",
    "def enumeration(dirname):\n",
    "    for root,dirs,files in os.walk(dirname):        \n",
    "        for file in files:            \n",
    "            pathList.append(os.path.join(root,file))\n",
    "        for dir in dirs:            \n",
    "            enumeration(dir)   \n",
    "            \n",
    "def enumeration2(dirname):\n",
    "    for root,dirs,files in os.walk(dirname):        \n",
    "        for file in files:            \n",
    "            if \".csv\" in os.path.join(root,file) and \"信息统计\" not in os.path.join(root,file):\n",
    "                pathList2.append(os.path.join(root,file))\n",
    "        for dir in dirs:            \n",
    "            enumeration2(dir)          \n",
    "            \n",
    "def enumeration3(dirname):\n",
    "    for root,dirs,files in os.walk(dirname):        \n",
    "        for file in files:            \n",
    "            if \".png\" in os.path.join(root,file) and \"类型\" in os.path.join(root,file):\n",
    "                pathList3.append(os.path.join(root,file))\n",
    "        for dir in dirs:            \n",
    "            enumeration3(dir)          \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "pathList = []   \n",
    "pathList2 = []\n",
    "pathList3 = [] #斜率列表\n",
    "enumeration(area_csv_path)#区域csv\n",
    "enumeration2(csv_path) #csv路径\n",
    "\n",
    "pathList4 = pathList[:]\n",
    "checklist = pathList[:]\n",
    "countttt = 0\n",
    "starttime = time.time()\n",
    "timenow = time.time()\n",
    "print(\"预估完成时间：犹未可知\")\n",
    "while pathList:\n",
    "    file = pathList[0]\n",
    "    title = file.split(\"\\\\\")[-1].split(\"_\")[-2]\n",
    "    area_word = file.split(\"\\\\\")[-1].split(\"_\")[-1]\n",
    "    if \"交错\" in title:\n",
    "        templist = title.replace(\"交错带\",\"\").split(\"-\")\n",
    "        temp = \".\".join(str(tempr.index(i)) for i in templist)\n",
    "    else:\n",
    "        temp = str(tempr.index(title))\n",
    "\n",
    "    if \"7\" in temp:\n",
    "        max_limit = 30\n",
    "\n",
    "        loc = loc.replace(\"left\",\"right\")\n",
    "    elif \"6\" in temp:\n",
    "        max_limit = 30\n",
    "        loc = loc.replace(\"left\",\"right\")\n",
    "    else:\n",
    "        max_limit = 22\n",
    "\n",
    "    if \".\" in temp: #交错带\n",
    "        area = \"\".join(i for i in area_word if 48<=ord(i)<=57)\n",
    "        start = \"0.\" + area +\".0.\"\n",
    "        word = \"_地域\" +str(area)+\"_\"\n",
    "        temp_word =\"_\" +  \"-\".join(tempr[int(i)] for i in temp.split(\".\")) + \"交错带\"\n",
    "        release_csv_list = [csv_path + \"\\\\\" + i for i in k_dict if i.startswith(start)]\n",
    "\n",
    "        release_csv_list = [i for i in release_csv_list if i.split(\"_\")[-1].replace(\".csv\",\"\") == temp]\n",
    "     \n",
    "        if len(release_csv_list) == 0:\n",
    "            pathList.pop(0)\n",
    "            continue\n",
    "        release_csv_list.sort(key = lambda x : int(x.split(\"\\\\\")[-1].replace(start,\"\" ).split(\"_\")[0]))\n",
    "        temp_sign = False\n",
    "    else: \n",
    "        area = \"\".join(i for i in area_word if 48<=ord(i)<=57)\n",
    "        start = temp + \".\" + area +\".0.\"\n",
    "        word =  \"_地域\" +str(area)+\"_\"\n",
    "        release_csv_list = [csv_path + \"\\\\\" + i for i in k_dict if i.startswith(start)]\n",
    "        if len(release_csv_list) == 0:\n",
    "            pathList.pop(0)\n",
    "            continue\n",
    "\n",
    "        release_csv_list.sort(key = lambda x : int(x.split(\"\\\\\")[-1].replace(start,\"\" ).replace(\".csv\",\"\")))\n",
    "     \n",
    "        w1 = \"\\\\\" + temp + \"_\" +tempr[int(temp)] + \"\\\\\"\n",
    "        w2 = \"\\\\\" + temp +tempr[int(temp)] + \"\\\\\"\n",
    "        w3 = \"\\\\\" + tempr[int(temp)] + \"\\\\\"\n",
    "        temp_sign = int(temp)\n",
    "    if type_dict and area not in type_dict:\n",
    "        pathList.pop(0)\n",
    "        continue \n",
    "\n",
    "    if k_dict_total:\n",
    "        e = ExecuteCsv_rewrite(file,name2 + \" \" + title,word,img_save_path,release_csv_list) \n",
    "        e.set_save_path(img_save_path + \"\\\\格子图\")         \n",
    "        print(\"格子图拟合\")\n",
    " \n",
    "        e.start_execute(name2 + \" \" + title,release_csv_list,area,max_limit,temp_sign,k_dict)\n",
    "\n",
    "        e.set_save_path(img_save_path + \"\\\\总体拟合（格子图）\")\n",
    "        print(\"格子图总体拟合\")\n",
    "        e.start_execute(name2 + \" \" + title,release_csv_list,area,max_limit,temp_sign,k_dict_total)\n",
    "\n",
    "    else:\n",
    "        e = ExecuteCsv_rewrite(file,name2 + \" \" + title,word,img_save_path,release_csv_list) \n",
    "        e.set_save_path(img_save_path + \"\\\\分段拟合（格子图）\")         \n",
    "        print(\"格子图分段拟合\")\n",
    "        e.start_execute(name2 + \" \" + title,release_csv_list,area,max_limit,temp_sign,k_dict)\n",
    "        \n",
    "        \n",
    "    time1 = time.time()\n",
    "    estimate = (time1-timenow)*(len(pathList2)-countttt-len(release_csv_list))/len(release_csv_list)\n",
    "    timenow = time1\n",
    "    countttt += len(release_csv_list)\n",
    "    print(\"进度%s/%s，预估完成时间%s\"%(countttt,len(pathList2),time.ctime(time.time()+estimate)))\n",
    "\n",
    "    gc.collect() \n",
    "    pathList.pop(0)\n",
    "\n",
    "pathList = pathList4\n",
    "print(\"end\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "46dff9b7",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (2990329601.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\xiong\\AppData\\Local\\Temp\\ipykernel_16880\\2990329601.py\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    lsd = mp.ListDataFrames(mxd)\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "mxd = mp.MapDocument('CURRENT')\n",
    "lsd = mp.ListDataFrames(mxd)\n",
    "default_name = \"\"\n",
    "for df in lsd :\n",
    "    for layer in mp.ListLayers(df):\n",
    "        if \"_图斑\" in layer.name:\n",
    "            assert (not default_name or default_name == layer.name),\"有其他图斑图层存在，删掉保证mxd文件中图斑名称唯一\"\n",
    "            default_name = layer.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c81a011",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
